package com.xinxing.city.recovery.ui;

import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.DI;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.adapter.SetSortAdapter;
import com.xinxing.city.recovery.adapter.SetSortAdapterTwo;
import com.xinxing.city.recovery.adapter.VideoSelAdapter;
import com.xinxing.city.recovery.databinding.ActivitySetEleBinding;
import com.xinxing.city.recovery.entitys.BucketType;
import com.xinxing.city.recovery.entitys.Devena;
import com.xinxing.city.recovery.entitys.KernelAction;
import com.xinxing.city.recovery.entitys.KernelActions;
import com.xinxing.city.recovery.entitys.MenuBean;
import com.xinxing.city.recovery.interfaces.IDeviceKernel;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class SetEleActivity extends AppCompatActivity {
    private ActivitySetEleBinding binding;
    private IDeviceKernel deviceKernel;
    Gson gson;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_set_ele);
        deviceKernel = DI.getInstance(IDeviceKernel.class);
        gson = new Gson();
        initPage();
    }
    public void initPage(){
//功能排序
        String menuList_c = (String) SharedPreferencesUtils.getParam(this,"menu_list","");
        Type menuListType = new TypeToken<List<MenuBean>>() {}.getType();
        List<MenuBean> menuList = new ArrayList<>();
        if (!menuList_c.equals("")) menuList = gson.fromJson(menuList_c,menuListType);
        binding.gnCon.removeAllViews();
        for (int i = 0;i < menuList.size();i++) {
            SetSortAdapter setSortAdapter = new SetSortAdapter(this,menuList,i);
            binding.gnCon.addView(setSortAdapter);
        }

        //桶排序
        List<BucketType> bucketList = new ArrayList<>();
        /*桶*/
        String bucketStr = (String) SharedPreferencesUtils.getParam(this, "bucket", "");
        if (!bucketStr.equals("")) {
            Type dev = new TypeToken<List<BucketType>>() {}.getType();
            bucketList = gson.fromJson(bucketStr, dev);
        }


        binding.tCon.removeAllViews();
        for (int i = 0;i < bucketList.size();i++) {
            SetSortAdapterTwo setSortAdapterTwo = new SetSortAdapterTwo(this,bucketList,i);
            binding.tCon.addView(setSortAdapterTwo);
        }

        //摄像头
        int cameraNumbers = Camera.getNumberOfCameras();
        int allnum = cameraNumbers;
        Devena devena;
        String devenaStr = (String) SharedPreferencesUtils.getParam(this, "set_devena", "");
        Gson gson = new Gson();
        if (!devenaStr.equals("")) {
            Type dev = new TypeToken<Devena>() {
            }.getType();
            devena = gson.fromJson(devenaStr, dev);
        } else {
            devena = new Devena();
        }
        if(devena.isAroundCamera()){
            allnum = allnum + 1;
        }
        for (int i = 0;i < allnum;i++) {
            VideoSelAdapter videoSelAdapter = new VideoSelAdapter(this,i,cameraNumbers + 1);
            binding.videoCon.addView(videoSelAdapter);
        }

    }
    public void cz(View view) {
        SharedPreferencesUtils.setParam(this,"menu_list","");
        deviceKernel.initMenu();
        initPage();
    }
    //视频显示
    public void videoSH(View view) {
        if (binding.selEleVideo.getVisibility() == View.VISIBLE) {
            binding.selEleVideo.setVisibility(View.GONE);
        } else {
            binding.selEleVideo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        EventBus.getDefault().post(new KernelAction(KernelActions.INIT_FINISH, null));
    }
}
